require "common/Define"
require "framework/BaseController"
require "FairyGUI"
require "GlobalWaiting"
require "Window4"

ModalWaitingMain = BaseController:New({mControllerName = "ModalWaitingMain"});

function ModalWaitingMain:Main()
	-- body
	ModalWaitingMain:Awake();
end

function ModalWaitingMain:Awake()
	UIPackage.AddPackage("UI/ModalWaiting");
	UIConfig.globalModalWaiting = "ui://ModalWaiting/GlobalModalWaiting";
	UIConfig.windowModalWaiting = "ui://ModalWaiting/WindowModalWaiting";

	-- UIObjectFactory.SetPackageItemExtension("ui://ModalWaiting/GlobalModalWaiting", typeof(GlobalWaiting));
	fgui.register_extension("ui://ModalWaiting/GlobalModalWaiting", GlobalWaiting);

	-- setting
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- load ui
	local prePackageName = "UI/";
	local packageName = "ModalWaiting";
	UIPackage.AddPackage(prePackageName..packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject(packageName, componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	-- Coroutine todo
	StartCoroutine(self.CoroutineFun);

	--
	self._testWin = Window4.New();
	self.mainComponent:GetChild("n0").onClick:Add( function () ModalWaitingMain._testWin:Show(); end );
end

function ModalWaitingMain:CoroutineFun()
	print("CoroutineFun...");
	GRoot.inst:ShowModalWait();
	print("Before 3 seconds...");
	WaitForSeconds(3.0);
	print("After 3 seconds...");
	GRoot.inst:CloseModalWait();
end


function ModalWaitingMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end
